package com.hsd.znsh.service.impl;

import com.hsd.znsh.converter.HostConverter;
import com.hsd.znsh.entity.Host;
import com.hsd.znsh.entity.User;
import com.hsd.znsh.enums.DeletedStatusEnum;
import com.hsd.znsh.pojo.input.HostSetting;
import com.hsd.znsh.pojo.output.HostGeolocationVO;
import com.hsd.znsh.pojo.output.HostVO;
import com.hsd.znsh.repository.HostManagerRepository;
import com.hsd.znsh.repository.HostRepository;
import com.hsd.znsh.repository.UserRepository;
import com.hsd.znsh.service.HostService;
import com.hsd.znsh.utils.BeanUtil;
import com.hsd.znsh.utils.HqlUtil;
import com.hsd.znsh.utils.SecurityUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;

/*
 * @auther: wangjiacai
 *
 * @create: 2018-04-25 09:57:10
 */
@Service
public class HostServiceImpl extends BaseServiceImpl implements HostService {

    @Autowired
    private HostRepository hostRepository;

    @Autowired
    private UserRepository userRepository;

    @Autowired
    private HostManagerRepository hostManagerRepository;

    @Override
    public List<Host> getAllHostList(Integer userId) {
        if(userId==null) return hostRepository.findByDeleted(DeletedStatusEnum.NOT_DELETED.getCode());
        return hostRepository.findByManagerIdAndDeleted(userId,DeletedStatusEnum.NOT_DELETED.getCode());
    }


    @Override
    public List<HostVO> getHostListByAPPUser(User user) {
        List<Integer> hostIds=hostManagerRepository.findBySecondManagerId(user.getId());
        List<Host> list=hostRepository.findByManagerIdAndDeleted(user.getCreateUserId(),DeletedStatusEnum.NOT_DELETED.getCode());
        list.addAll(hostRepository.findByIdIn(hostIds));
        return HostConverter.converterToHostVOList(list,user);
    }

    @Override
    public List<Host> getOwnHostListByAPPUser(User user) {
        List<Host> list=hostRepository.findByManagerIdAndDeleted(user.getCreateUserId(),DeletedStatusEnum.NOT_DELETED.getCode());
        return list;
    }

    @Override
    public List<Host> getManagerHostList(String namagerName) {
        User user=userRepository.findByUsername(namagerName);
        List<Integer> hostIds=hostManagerRepository.findBySecondManagerId(user.getId());
        List<Host> list=hostRepository.findByIdIn(hostIds);
        return list;
    }

    @Override
    public Host getHostInfoByHostSn(String hostSn) {
        return hostRepository.findByHostSn(hostSn);
    }

    @Override
    public Host updateHostSettingsInfoByHostSn(HostSetting hostSetting) {
        Host host=hostRepository.findByHostSn(hostSetting.getHostSn());
        BeanUtil.copyPropertiesIgnoreNull(hostSetting,host);
        return hostRepository.save(host);
    }


    @Override
    public long getHostListTotalPages(Host host, Integer userId) {
        if(userId!=null) host.setManagerId(userId);
        return this.count(HqlUtil.getCountHql(host),host);
    }

    @Override
    public List<HostVO> getHostList(Pageable pageable, Host host, Integer userId) {
        if(userId!=null) host.setManagerId(userId);
        List<Host> list=this.find(HqlUtil.getQueryHql(host),host,pageable);
        return HostConverter.converterToHostVOList(list,userRepository);
    }

    @Override
    public Host addHost(Host host) {
        if(SecurityUtil.hasRole("BACKGROUND_MANAGER")){
            host.setManagerId(SecurityUtil.getUserId());
        }
        host.setAlias(host.getHostName());
        return hostRepository.save(host);
    }

    @Override
    public Host updateHost(Host host) {
        Host target=hostRepository.findOne(host.getId());
        BeanUtil.copyPropertiesIgnoreNull(host,target);
        return hostRepository.save(target);
    }

    @Override
    public HostVO getHostInfo(Integer id) {
        Host host=hostRepository.findOne(id);
        return HostConverter.converterToHostVO(host,userRepository);
    }

    @Override
    public void deletedHost(Integer id) {
        Host host=hostRepository.findOne(id);
        host.setDeleted(DeletedStatusEnum.DELETED.getCode());
        hostRepository.save(host);
    }

    @Override
    public void updateCrop(String hostSn, String name, String value) {
        Host host=hostRepository.findByHostSn(hostSn);
        BeanUtil.setValueByPropertyNames(host,name,value);
        hostRepository.save(host);
    }
}
